package com.algrithom.quene;

import java.util.Arrays;

import lombok.Data;

/**
 * 数组形式的循环队列
 *
 * @author think
 * @version 1.0.0
 * @since 2020/1/14
 */
@Data
public class CircleQueue1 {
    
    private int[] data;
    
    private int head;
    
    private int tail;
    
    private int size;
    
    public CircleQueue1(int k){
        data = new int[k];
        head = -1;
        tail = -1;
        size = k;
    }
    
    public static void main(String[] args){
        int space = 5;
        CircleQueue1 circleQueue = new CircleQueue1(space);
        System.out.println(circleQueue.enQueue(1));
        System.out.println(circleQueue.enQueue(2));
        System.out.println(circleQueue.enQueue(3));
        System.out.println(circleQueue.enQueue(4));
        System.out.println(circleQueue.enQueue(5));
        System.out.println(circleQueue.enQueue(6));
        System.out.println(circleQueue.deQueue());
        System.out.println(circleQueue.enQueue(6));
        System.out.println(circleQueue.isEmpty());
        System.out.println(circleQueue.isFull());
        System.out.println(Arrays.toString(circleQueue.getData()));
    }
    
    public boolean enQueue(int value){
        if (isFull()) {
            return false;
        }
        if (isEmpty()) {
            head = 0;
        }
        tail = (tail + 1) % size;
        data[tail] = value;
        return true;
    }
    
    public boolean deQueue(){
        if (isEmpty()) {
            return false;
        }
        if (head == tail) {
            head = -1;
            tail = -1;
            return true;
        }
        head = (head + 1) % size;
        return true;
    }
    
    public int front(){
        if (isEmpty()) {
            return -1;
        }
        return data[head];
    }
    
    public int rear(){
        if (isEmpty()) {
            return -1;
        }
        return data[tail];
    }
    
    public boolean isEmpty(){
        return head == -1;
    }
    
    public boolean isFull(){
        return ((tail + 1) % size) == head;
    }
}
